﻿#include "newwindow.h"
#include "ui_newwindow.h"
#include <QIcon>
#include <QGridLayout>
#include <QLabel>
#include <QRadioButton>
#include <QPushButton>
#include <QHBoxLayout>
#include <QFileDialog>

newWindow::newWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::newWindow)
{
    ui->setupUi(this);
    init();
}

newWindow::~newWindow()
{
    delete ui;
}

void newWindow::init()
{
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowFlags(Qt::Window|Qt::WindowCloseButtonHint);
    icon=new QIcon(":/title.ico");
    setWindowIcon(*icon);
    setWindowTitle("新建工程");
    initConnect();

    setModelHide();
}

void newWindow::initConnect()
{

}

void newWindow::setModelHide()
{
    ui->label_model->hide();
    ui->lineEdit_model->hide();
    ui->pushButton_model->hide();
}

void newWindow::setModelShow()
{
    ui->label_model->show();
    ui->lineEdit_model->show();
    ui->pushButton_model->show();
}



void newWindow::on_radioButton_new_clicked()
{
    setModelHide();
}

void newWindow::on_radioButton_model_clicked()
{
    setModelShow();
}

void newWindow::on_pushButton_model_clicked()
{
    w=new QWidget;
    //w->setAttribute(Qt::WA_DeleteOnClose);
    w->setWindowIcon(*icon);
    w->setWindowTitle("选择模版");
    w->setWindowFlags(Qt::Window|Qt::WindowCloseButtonHint);
    setModelGui(w);
    w->show();
}

void newWindow::on_BtnOk()
{
    for (QAbstractButton *button : group->buttons())
    {
       if (button->isChecked())
       {
           ui->lineEdit_model->setText(button->text());
           break;
       }
     }

    w->close();
}

void newWindow::on_BtnCancel()
{
    w->close();
}

void newWindow::setModelGui(QWidget*&w)
{
     QGridLayout*layout=new QGridLayout();
     QLabel *labels=new QLabel[6];
     for(int i=0;i<6;i++)
     {
         QPixmap pix(QString(":/images/label%1.jpg").arg(i));
         labels[i].setPixmap(pix);
         labels[i].setFixedSize(QSize(300,250));
         labels[i].setScaledContents(true);
     }
     group=new QButtonGroup;
     QRadioButton*rBtns=new QRadioButton[6];
     for(int i=0;i<6;i++)
     {
         rBtns[i].setText(QString("模版%1").arg(i+1));
         group->addButton(&rBtns[i]);
     }


    layout->addWidget(&labels[0],0,0);
    layout->addWidget(&labels[1],0,1);
    layout->addWidget(&labels[2],0,2);
    layout->addWidget(&rBtns[0],1,0);
    layout->addWidget(&rBtns[1],1,1);
    layout->addWidget(&rBtns[2],1,2);

    layout->addWidget(&labels[3],2,0);
    layout->addWidget(&labels[4],2,1);
    layout->addWidget(&labels[5],2,2);
    layout->addWidget(&rBtns[3],3,0);
    layout->addWidget(&rBtns[4],3,1);
    layout->addWidget(&rBtns[5],3,2);


    rBtns[0].setChecked(true);

    QPushButton*BtnOk=new QPushButton("确定");
    QPushButton*BtnCancel=new QPushButton("取消");
    QHBoxLayout *Hlayout=new QHBoxLayout;
    //Hlayout->addStretch();
    Hlayout->addWidget(BtnOk);
    Hlayout->addWidget(BtnCancel);
    layout->addLayout(Hlayout,4,2);

    w->setLayout(layout);

    connect(BtnOk,SIGNAL(clicked()),this,SLOT(on_BtnOk()));
    connect(BtnCancel,SIGNAL(clicked()),this,SLOT(on_BtnCancel()));



//     for(int index=0;index<6;index++)
//     {
//         for(int i=0;i<4;i+=2)
//         {
//             for(int j=0;j<3;j++)
//             {
//                 layout->addWidget(&labels[index],i,j);
//             }
//         }
//     }
//     for(int index=0;index<6;index++)
//     {
//         for(int i=1;i<4;i+=2)
//         {
//             for(int j=0;j<3;j++)
//             {
//                 layout->addWidget(&rBtns[index],i,j);
//             }
//         }
//     }

//     int index=0;
//     int index2=0;
//     for(int i=0;i<2;i++)
//     {
//         for(int j=0;j<3;j++)
//         {

//             layout->addWidget(&labels[index++],i,j);
//             layout->addWidget(&rBtns[index2++],++i,j);
//         }
//     }
}

void newWindow::on_pushButton_path_clicked()
{
    QString filePath=QFileDialog::getOpenFileName(this,"选择工程",QDir::currentPath(),"所有文件(*);;图片文件(*.png *.jpg *.jpeg)");
    if(!filePath.isEmpty())
    {
        ui->lineEdit_path->setText(filePath);
    }
}

void newWindow::on_comboBox_ratio_currentIndexChanged(const QString &arg1)
{
    QStringList list=arg1.split("*");
    if(list.length()>1)
    {
        ui->lineEdit_width->setText(list.at(0));
        ui->lineEdit_height->setText(list.at(1));
    }
    else
    {
        ui->lineEdit_width->clear();
        ui->lineEdit_height->clear();
    }

}

void newWindow::on_pushButton_cancel_clicked()
{
    close();
}

void newWindow::on_pushButton_ok_clicked()
{
    emit dataReady(ui->lineEdit_name->text(),ui->lineEdit_path->text(),
                   ui->lineEdit_width->text(),ui->lineEdit_height->text(),ui->lineEdit_info->text());
    close();
}
